package gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;

import common.Mail;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;

/**
 * Gestor del panel de compra.
 *
 */
public class PanelCompra extends JPanel {
	private JTextField nombre;
	private JTextField apellidos;
	private JTextField dni;
	private JTextField direccion;
	private JTextField email;
	private JTextField tarjeta;
	private JTextField fecha;
	private JTextField cvv;
	private String totalS ="";
	
	/**
	 * Cierra el panel de la compra
	 */
	private void salir(){
		this.setVisible(false);
		MainWindow.frameComprar.setVisible(false);
		MainWindow.frameComprar.revalidate();
	}
	/**
	 * 
	 * @param totl Precio al que asciende la compra
	 * @param mw Pantalla principal del catalogo
	 */
	public PanelCompra(double totl, MainWindow mw) {
		DecimalFormat decimales = new DecimalFormat("0.00");  
		 String precio= decimales.format(totl);
		this.totalS = precio+" euros";

		JLabel lblNombre = new JLabel("Nombre:");
		
		JLabel lblApellidos = new JLabel("Apellidos:");
		
		JLabel lblDireccion = new JLabel("Direccion:");
		
		JLabel lblEmail = new JLabel("E-mail:");
		
		JLabel lblTarjetaDeCredito = new JLabel("Tarjeta de credito:");
		
		JLabel lblFechaCaducidad = new JLabel("Fecha caducidad:");
		
		JLabel lblCvv = new JLabel("CVV:");
		
		JLabel lblDni = new JLabel("DNI:");
		
		nombre = new JTextField();
		nombre.setColumns(10);
		
		apellidos = new JTextField();
		apellidos.setColumns(10);
		
		dni = new JTextField();
		dni.setText("77777777X");
		dni.setColumns(10);
		
		direccion = new JTextField();
		direccion.setText("Avda/ Sin calle, N\u00BA1");
		direccion.setColumns(10);
		
		email = new JTextField();
		email.setColumns(10);
		
		tarjeta = new JTextField();
		tarjeta.setText("5111-1111-2222-2269");
		tarjeta.setColumns(10);
		
		fecha = new JTextField();
		fecha.setText("03/2015");
		fecha.setColumns(10);
		
		cvv = new JTextField();
		cvv.setText("293");
		cvv.setColumns(10);
		
		JLabel lblTotal = new JLabel("TOTAL:");
		
		JLabel total = new JLabel(totalS);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				salir();
			}
		});
		
		JButton btnAceptar = new JButton("Aceptar");
		final MainWindow m = mw;
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Mail mailOb = new Mail();
				String nom = nombre.getText();
				String apel = apellidos.getText();
				String dir = direccion.getText();
				String mai = email.getText();
				String prec = totalS.substring(0,totalS.length()-1);
				
				String texto = "<h3>Hola "+nom+" "+apel+",</br></br>"+"su pago de "+prec+" euros se ha realizado correctamente. </br>Sera enviado a la siguiente direccion:</br><b>"+
				dir+"</b></br></br> Un saludo, Catspace.</h3>";
				if(nom==null || nom.equals("") || apel==null || apel.equals("") || dir==null || dir.equals("") || mai==null || mai.equals("") || prec==null || prec.equals("")){
					JOptionPane.showMessageDialog(null,
							"Falta algun campo","ERROR",JOptionPane.ERROR_MESSAGE);
				}
				else if(!mai.contains("@")){
					JOptionPane.showMessageDialog(null,
							"Email no valido","ERROR",JOptionPane.ERROR_MESSAGE);
				}else{
					mailOb.enviaMail(mai, texto);
					// Vaciar cesta
					m.vaciarCesta();
					
					JOptionPane.showMessageDialog(null,
							"Pago realizado con exito!");
					salir();
				}
			}
		});
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblTarjetaDeCredito)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(tarjeta, GroupLayout.PREFERRED_SIZE, 205, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblEmail)
							.addGap(18)
							.addComponent(email, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblTotal)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(total))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNombre)
								.addComponent(lblApellidos)
								.addComponent(lblDni))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(dni, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(apellidos, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE)
								.addComponent(nombre, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblCvv)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(cvv, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblFechaCaducidad)
								.addGap(18)
								.addComponent(fecha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(btnCancelar)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblDireccion)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(direccion, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnAceptar)))
					.addGap(187))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNombre)
						.addComponent(nombre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(13)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblApellidos)
						.addComponent(apellidos, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDni)
						.addComponent(dni, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(10)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDireccion)
						.addComponent(direccion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEmail)
						.addComponent(email, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(25)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTarjetaDeCredito)
						.addComponent(tarjeta, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblFechaCaducidad)
						.addComponent(fecha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCvv)
						.addComponent(cvv, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(31)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTotal)
						.addComponent(total))
					.addPreferredGap(ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCancelar)
						.addComponent(btnAceptar))
					.addContainerGap())
		);
		setLayout(groupLayout);

	}
}
